/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.Service
*        文件名:             StaffService.svc.cs
*        创建系统时间:       2012/11/2 19:07:06
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.Service
{
    using System;
    using System.Data;
    using System.Reflection;
    using System.ServiceModel;
    using ECP.DataAccess;
    using ECP.DataAccess.Utilities;
    using ECP.Interface;
    using ECP.Model;
    using ECP.Utilities;

    [ServiceBehavior(Namespace = "http://jiangguang.net.cn/")]
    public partial class StaffService : MarshalByRefObject, IStaffService
    {
        private static StaffService instance = null;
        private static object locker = new object();

        public string AddStaff(BaseUserInfo userInfo, string companyID, string departmentID, string workgroupID, string dutyID, string titleID, string code, string realName, bool isUser, string userName, string roleID, string email, string password, string sex, bool enabled, string description, out string statusCode, out string statusMessage)
        {
            string str = string.Empty;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                string userID = null;
                if (isUser)
                {
                    string company = null;
                    BaseOrganizeDao dao = new BaseOrganizeDao(dbHelper, userInfo);
                    if (!string.IsNullOrEmpty(companyID))
                    {
                        company = dao.GetProperty(companyID, BaseOrganizeTable.FieldFullName);
                    }
                    string department = null;
                    if (!string.IsNullOrEmpty(departmentID))
                    {
                        department = dao.GetProperty(departmentID, BaseOrganizeTable.FieldFullName);
                    }
                    string workgroup = null;
                    if (!string.IsNullOrEmpty(workgroupID))
                    {
                        workgroup = dao.GetProperty(departmentID, BaseOrganizeTable.FieldFullName);
                    }
                    userID = UserService.Instance.AddUser(dbHelper, userInfo, company, department, workgroup, userName, realName, code, roleID, password, email, enabled, description, out statusCode, out statusMessage);
                    if (!statusCode.Equals(StatusCode.OKAdd.ToString()))
                    {
                        return str;
                    }
                }
                BaseStaffDao dao2 = new BaseStaffDao(dbHelper, userInfo);
                str = dao2.AddStaff(companyID, departmentID, dutyID, titleID, userName, realName, code, roleID, sex, userID, enabled, description, out statusCode);
                statusMessage = dao2.GetStateMessage(statusCode);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return str;
        }

        public int BatchDelete(BaseUserInfo userInfo, string[] ids)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseStaffDao(dbHelper, userInfo).BatchDelete(ids);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int BatchMoveTo(BaseUserInfo userInfo, string[] ids, string organizeID)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseStaffDao dao = new BaseStaffDao(dbHelper, userInfo);
                for (int i = 0; i < ids.Length; i++)
                {
                    num += dao.SetProperty(ids[i], BaseStaffTable.FieldDepartmentID, organizeID);
                }
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int BatchSave(BaseUserInfo userInfo, DataTable dataTable)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseStaffDao(dbHelper, userInfo).BatchSave(dataTable);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int ChangeDepartment(BaseUserInfo userInfo, string id, string companyID, string departmentID)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseStaffDao dao = new BaseStaffDao(dbHelper, userInfo);
                string[] targetFields = new string[2];
                object[] targetValues = new object[2];
                targetFields[0] = BaseStaffTable.FieldCompanyID;
                targetValues[0] = companyID;
                targetFields[1] = BaseStaffTable.FieldDepartmentID;
                targetValues[1] = departmentID;
                num = dao.SetProperty(id, targetFields, targetValues);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int Delete(BaseUserInfo userInfo, string id)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseStaffDao(dbHelper, userInfo).Delete(id);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int DeleteUser(BaseUserInfo userInfo, string staffID)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseStaffDao(dbHelper, userInfo).DeleteUser(staffID);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public DataTable Get(BaseUserInfo userInfo, string id)
        {
            DataTable dT = new DataTable(BaseStaffTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                dT = new BaseStaffDao(dbHelper, userInfo).GetDT(BaseStaffTable.FieldID, id);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return dT;
        }

        public DataTable GetAddressList(BaseUserInfo userInfo, string organizeID, string search)
        {
            DataTable addressDT = new DataTable(BaseStaffTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                addressDT = new BaseStaffDao(dbHelper, userInfo).GetAddressDT(organizeID, search);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return addressDT;
        }

        public DataTable GetDT(BaseUserInfo userInfo)
        {
            DataTable dT = new DataTable(BaseStaffTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                dT = new BaseStaffDao(dbHelper, userInfo).GetDT();
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return dT;
        }

        public DataTable GetDTByDepartment(BaseUserInfo userInfo, string departmentID, bool containChildren)
        {
            DataTable childrenStaffs = new DataTable(BaseStaffTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseStaffDao dao = new BaseStaffDao(dbHelper, userInfo);
                if (containChildren)
                {
                    childrenStaffs = new BaseOrganizeDao(dbHelper, userInfo).GetChildrenStaffs(departmentID);
                }
                else
                {
                    childrenStaffs = dao.GetDTByDepartment(departmentID);
                }
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return childrenStaffs;
        }

        public DataTable GetDTByIDs(BaseUserInfo userInfo, string[] ids)
        {
            DataTable table = new DataTable(BaseStaffTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                table = new BaseStaffDao(dbHelper, userInfo).GetDT(BaseStaffTable.FieldID, (object[])ids, BaseStaffTable.FieldSortCode);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return table;
        }

        public DataTable GetDTByOrganize(BaseUserInfo userInfo, string organizeID, bool containChildren)
        {
            DataTable childrenStaffs = new DataTable(BaseStaffTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                string sqlQuery = "UPDATE ECP_Staff SET UserID = NULL WHERE (UserID NOT IN (SELECT ID FROM ECP_User))";
                dbHelper.ExecuteNonQuery(sqlQuery);
                BaseStaffDao dao = new BaseStaffDao(dbHelper, userInfo);
                if (containChildren)
                {
                    childrenStaffs = new BaseOrganizeDao(dbHelper, userInfo).GetChildrenStaffs(organizeID);
                }
                else
                {
                    childrenStaffs = dao.GetDTByOrganize(organizeID);
                }
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return childrenStaffs;
        }

        public void Load()
        {
        }

        public int MoveTo(BaseUserInfo userInfo, string id, string organizeID)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseStaffDao(dbHelper, userInfo).SetProperty(id, BaseStaffTable.FieldDepartmentID, organizeID);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int ResetSortCode(BaseUserInfo userInfo)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseStaffDao(dbHelper).ResetSortCode();
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public DataTable Search(BaseUserInfo userInfo, string organizeID, string search)
        {
            DataTable table = new DataTable(BaseStaffTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                table = new BaseStaffDao(dbHelper, userInfo).Search(organizeID, search, false);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return table;
        }

        public int SetStaffUser(BaseUserInfo userInfo, string staffID, string userID)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseStaffDao dao = new BaseStaffDao(dbHelper, userInfo);
                string[] iDs = dao.GetIDs(BaseStaffTable.FieldUserID, userID);
                if ((iDs == null) || (iDs.Length == 0))
                {
                    num = dao.SetProperty(staffID, BaseStaffTable.FieldUserID, userID);
                }
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int UpdateAddressList(BaseUserInfo userInfo, DataTable dataTable, out string statusCode, out string statusMessage)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                statusCode = string.Empty;
                BaseStaffEntity staffEntity = new BaseStaffEntity();
                BaseStaffDao dao = new BaseStaffDao(dbHelper, userInfo);
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    staffEntity.GetFrom(dataTable.Rows[i]);
                    num += dao.UpdateAddress(staffEntity, out statusCode);
                }
                statusMessage = dao.GetStateMessage(statusCode);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int UpdateStaff(BaseUserInfo userInfo, string id, string companyID, string departmentID, string workgroupID, string dutyID, string titleID, string code, string realName, bool isUser, string userID, string userName, string role, string email, string sex, bool enabled, string description, out string statusCode, out string statusMessage)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                if (isUser)
                {
                    string company = null;
                    BaseOrganizeDao dao = new BaseOrganizeDao(dbHelper, userInfo);
                    if (!string.IsNullOrEmpty(companyID))
                    {
                        company = dao.GetProperty(companyID, BaseOrganizeTable.FieldFullName);
                    }
                    string department = null;
                    if (!string.IsNullOrEmpty(departmentID))
                    {
                        department = dao.GetProperty(departmentID, BaseOrganizeTable.FieldFullName);
                    }
                    string workgroup = null;
                    if (!string.IsNullOrEmpty(workgroupID))
                    {
                        workgroup = dao.GetProperty(workgroupID, BaseOrganizeTable.FieldFullName);
                    }
                    BaseUserDao dao2 = new BaseUserDao(dbHelper, userInfo);
                    if (string.IsNullOrEmpty(userID))
                    {
                        string password = null;
                        userID = dao2.AddUser(company, department, workgroup, userName, realName, code, role, password, email, enabled, description, out statusCode);
                        statusMessage = dao2.GetStateMessage(statusCode);
                        if (!statusCode.Equals(StatusCode.OKAdd.ToString()))
                        {
                            return num;
                        }
                    }
                    else
                    {
                        num = dao2.UpdateUser(userID, userName, realName, code, role, enabled, out statusCode);
                        statusMessage = dao2.GetStateMessage(statusCode);
                        if (!statusCode.Equals(StatusCode.OKUpdate.ToString()))
                        {
                            return num;
                        }
                        dao2.SetProperty(userID, BaseUserTable.FieldCompany, company);
                        dao2.SetProperty(userID, BaseUserTable.FieldDepartment, department);
                        dao2.SetProperty(userID, BaseUserTable.FieldWorkgroup, workgroup);
                    }
                }
                BaseStaffDao dao3 = new BaseStaffDao(dbHelper, userInfo);
                num = dao3.UpdateStaff(id, companyID, departmentID, workgroupID, dutyID, titleID, code, realName, userID, userName, sex, enabled, description, out statusCode);
                statusMessage = dao3.GetStateMessage(statusCode);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public static StaffService Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new StaffService();
                        }
                    }
                }
                return instance;
            }
        }
    }
}